<template>
  <div v-loading="detailInfoShow">
    <div class="requestAdd" v-if="!selectionMaterialShow">
      <!-- 筛选按钮 -->
      <div class="search-button" v-if="!addShow">
        <template v-if="checkStatus != 3">
          <el-button
            class="btn save"
            @click="handleCheckBtn2(requestFormRef, 3, '同意')"
            ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
            >同意</el-button
          >
          <el-button
            class="btn withdraw"
            @click="handleCheckBtn2(requestFormRef, 4, '拒绝')"
            ><el-icon class="icon" color="#fff"> <DocumentDelete /> </el-icon
            >拒绝</el-button
          >
          <el-button
            class="btn withdraw"
            @click="handleCheckBtn2(requestFormRef, 6, '驳回')"
            ><el-icon class="icon" color="#fff"> <DocumentDelete /> </el-icon
            >驳回</el-button
          >
        </template>
        <el-button class="btn direction" @click="handleBack"
          ><el-icon class="icon" color="#fff"> <Back /> </el-icon
          >返回</el-button
        >
      </div>
      <el-collapse v-model="activeNames">
        <!-- 基础信息 -->
        <el-collapse-item name="1" v-if="!addShow" class="transform-down">
          <template #title>
            <span class="circle"></span>
            <span class="collapse-title"
              >{{
                pageType !== "detail"
                  ? pageType === "audit"
                    ? "审核"
                    : ""
                  : ""
              }}入库信息</span
            >
          </template>
          <div class="request-info">
            <el-form
              :model="requestForm"
              ref="requestFormRef"
              :rules="queryRules"
              label-width="100px"
              v-enterToNext
              :disabled="true"
            >
              <el-row>
                <el-col :span="6">
                  <el-form-item label="入库编号：">
                    <el-input
                      :disabled="true"
                      v-model="requestForm.warehousingApplyCode"
                      placeholder="自动获取"
                      clearable
                      class="search-input"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="入库批次：">
                    <el-input
                      :disabled="true"
                      style="width: 100%"
                      v-model="requestForm.batchCode"
                      placeholder="自动获取"
                      clearable
                      class="search-input"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="申请日期："
                    prop="applicantDate"
                    label-width="110"
                  >
                    <el-date-picker
                      v-model="requestForm.applicantDate"
                      :disabled="true"
                      class="search-input"
                      type="datetime"
                      placeholder="请选择"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="申请人："
                    prop="applicantId"
                    label-width="110"
                  >
                    <el-select
                      :disabled="true"
                      v-model="requestForm.applicantId"
                      placeholder="请选择"
                      clearable
                      style="width: 100%"
                      class="search-input"
                      @change="handleUserChange"
                    >
                      <el-option
                        v-for="dict in userList"
                        :key="dict.userId"
                        :label="dict.nickName"
                        :value="dict.userId"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="申请部门：">
                    <el-input
                      :disabled="true"
                      v-model="requestForm.deptName"
                      placeholder="自动获取"
                      clearable
                      class="search-input"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="入库类型：" prop="warehousingApplyType">
                    <el-select
                      v-model="requestForm.warehousingApplyType"
                      placeholder="请选择入库类型"
                      @change="handleApplyTypeChange"
                      clearable
                      style="width: 100%"
                      class="search-input"
                      :disabled="true"
                    >
                      <el-option
                        v-for="dict in inStatusList"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <!--0-借用入库 1-租赁入库 2-盘盈入库 3-返修入库 4-采购入库 5-其他  -->
                <el-col :span="12" v-if="requestForm.warehousingApplyType == 0">
                  <el-form-item
                    label="关联请购单："
                    label-width="110"
                    prop="purchaseApplyCode"
                  >
                    <el-autocomplete
                      ref="request"
                      popper-class="autoclass"
                      :disabled="true"
                      v-model="requestForm.purchaseApplyCode"
                      :fetch-suggestions="querySearch"
                      placeholder="请选择"
                      @focus="handleAutoCompleteFocus('request')"
                      @select="handleRequestSelect"
                      style="width: 100%"
                      clearable
                    >
                      <template #default="{ item }">
                        <!-- <div class="auto-top"> -->
                        <div class="auto-top" style="display: flex">
                          <div style="display: flex; width: 140px">
                            请购单编号
                          </div>
                          <div class="auto-item">申请人</div>
                          <div class="auto-item">关联项目</div>
                          <div class="auto-item">期望交付日期</div>
                        </div>
                        <div style="display: flex; width: 500px">
                          <div
                            class="auto-item"
                            :title="item.purchaseApplyCode"
                            style="display: flex; width: 170px"
                          >
                            {{ item.purchaseApplyCode }}
                          </div>
                          <div class="auto-item" :title="item.applicantName">
                            {{ item.applicantName }}
                          </div>
                          <div class="auto-item" :title="item.projectName">
                            {{ item.projectName }}
                          </div>
                          <div class="auto-item" :title="item.deliveryDate">
                            {{ item.deliveryDate }}
                          </div>
                        </div>
                      </template>
                    </el-autocomplete>
                  </el-form-item>
                </el-col>

                <el-col :span="12" v-if="requestForm.warehousingApplyType == 3">
                  <el-form-item
                    label="关联出库单："
                    label-width="110"
                    prop="deliveryApplyCode"
                  >
                    <el-autocomplete
                      popper-class="autoclass"
                      ref="outOrder"
                      v-model="requestForm.deliveryApplyCode"
                      :fetch-suggestions="querySearch"
                      :disabled="true"
                      placeholder="请选择"
                      @focus="handleAutoCompleteFocus('outOrder')"
                      @select="handleOutOrderSelect"
                      style="width: 100%"
                      clearable
                    >
                      <template #default="{ item }">
                        <!-- <div class="auto-top"> -->
                        <div class="auto-top" style="display: flex">
                          <div style="display: flex; width: 140px">
                            出库单编号
                          </div>
                          <div class="auto-item">申请人</div>
                        </div>
                        <div style="display: flex; width: 500px">
                          <div
                            class="auto-item"
                            :title="item.deliveryApplyCode"
                            style="display: flex; width: 170px"
                          >
                            {{ item.deliveryApplyCode }}
                          </div>
                          <div class="auto-item" :title="item.applicantName">
                            {{ item.applicantName }}
                          </div>
                        </div>
                      </template>
                    </el-autocomplete>
                  </el-form-item>
                </el-col>
                <el-col
                  :span="12"
                  v-if="
                    requestForm.warehousingApplyType == 1 ||
                    requestForm.warehousingApplyType == 4
                  "
                >
                  <el-form-item label="关联合同：" prop="contractCode">
                    <el-autocomplete
                      popper-class="autoclass"
                      ref="contract"
                      v-model="requestForm.contractCode"
                      :fetch-suggestions="querySearch"
                      :disabled="true"
                      placeholder="请选择"
                      @focus="handleAutoCompleteFocus('contract')"
                      @select="handleContractSelect"
                      style="width: 100%"
                      clearable
                    >
                      <template #default="{ item }">
                        <!-- <div class="auto-top"> -->
                        <div class="auto-top" style="display: flex">
                          <div style="display: flex; width: 170px">
                            合同编号
                          </div>
                          <div class="auto-item">合同名称</div>
                          <div class="auto-item">供应商</div>
                        </div>
                        <div style="display: flex; width: 500px">
                          <div
                            class="auto-item"
                            :title="item.purchaseContractCode"
                            style="display: flex; width: 170px"
                          >
                            {{ item.purchaseContractCode }}
                          </div>
                          <div
                            class="auto-item"
                            :title="item.purchaseContractName"
                          >
                            {{ item.purchaseContractName }}
                          </div>
                          <div class="auto-item" :title="item.supplierName">
                            {{ item.supplierName }}
                          </div>
                        </div>
                      </template>
                    </el-autocomplete>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="关联项目：">
                    <el-autocomplete
                      popper-class="autoclass"
                      ref="project"
                      v-model="requestForm.projectName"
                      :fetch-suggestions="querySearch"
                      :disabled="true"
                      placeholder="请选择"
                      @focus="handleAutoCompleteFocus('project')"
                      @select="handleProjectSelect"
                      style="width: 100%"
                      clearable
                    >
                      <template #default="{ item }">
                        <!-- <div class="auto-top"> -->
                        <div class="auto-top" style="display: flex">
                          <div style="display: flex; width: 140px">
                            项目编号
                          </div>
                          <div class="auto-item">项目名称</div>
                          <div class="auto-item">项目类别</div>
                          <div class="auto-item">项目类型</div>
                        </div>
                        <div style="display: flex; width: 500px">
                          <div
                            class="auto-item"
                            :title="item.projectCode"
                            style="display: flex; width: 170px"
                          >
                            {{ item.projectCode }}
                          </div>
                          <div class="auto-item" :title="item.projectName">
                            {{ item.projectName }}
                          </div>
                          <div
                            class="auto-item"
                            :title="item.projectCategoryName"
                          >
                            {{ item.projectCategoryName }}
                          </div>
                          <div class="auto-item" :title="item.projectTypeName">
                            {{ item.projectTypeName }}
                          </div>
                        </div>
                      </template>
                    </el-autocomplete>
                  </el-form-item>
                </el-col>
                <el-col
                  :span="12"
                  v-if="
                    requestForm.warehousingApplyType == 1 ||
                    requestForm.warehousingApplyType == 4
                  "
                >
                  <el-form-item label="供应商：">
                    <el-input
                      style="width: 100%"
                      v-model="requestForm.supplierName"
                      placeholder=""
                      type="text"
                      clearable
                      :disabled="true"
                    />
                  </el-form-item>
                </el-col>
                
                </el-row
              ><el-row>
                <el-col :span="12">
                  <el-form-item label="备注：" label-width="110">
                    <el-input
                      style="width: 100%"
                      :disabled="true"
                      v-model="requestForm.remarks"
                      placeholder=""
                      type="textarea"
                      clearable
                      maxlength="2000"
                    />
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item
                    label="附件："
                    prop="businessLicense"
                    label-width="110"
                  >
                    <Upload
                      :fileList="requestForm.fileList"
                      :isFull="true"
                      :isCanDelete="false"
                      :disabled="true"
                      :showButton="true"
                      @success="(result) => uploadSuccess(result, 8)"
                      @deleteFile="(result) => uploadRemove(result, 8)"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-collapse-item>
        <!-- 添加物料 -->

        <!-- 联系人列表 -->
        <el-collapse-item
          name="3"
          style="position: relative"
          :class="!addShow ? 'transform-down' : ''"
        >
          <template #title>
            <span class="circle"></span>
            <span class="collapse-title">物料列表</span>
            <div class="search-button button-position"></div>
          </template>
          <div class="contact-info">
            <!-- 表格数据 -->
            <el-table
              :data="requestForm.dtlList"
              border
              :height="'320px'"
              :header-cell-style="{ 'text-align': 'center' }"
              show-summary
              sum-text="合计"
              :summary-method="getSummaries"
            >
              <el-table-column
                label="操作"
                v-if="requestForm.warehousingCheckStatus == 3"
                align="center"
                :show-overflow-tooltip="true"
                width="100px"
              >
                <template #default="{ row, $index }">
                  <div
                    class="table-operate"
                    v-if="row.warehousingQuantity != row.inQty"
                  >
                    <el-button
                      style="background-color: #b0e0e6"
                      @click="handleMaterial(row, $index)"
                    >
                      入库
                    </el-button>
                  </div>
                  <div v-else>
                    <el-button disabled style="background-color: #ccc">
                      已入库
                    </el-button>
                  </div>
                </template>
              </el-table-column>
              <!-- 操作模式 -->

              <el-table-column
                label="序号"
                type="index"
                :show-overflow-tooltip="true"
                min-width="60"
              />
              <el-table-column
                label="物料编号"
                prop="materialCode"
                :show-overflow-tooltip="true"
                min-width="100"
              >
                <template #default="{ row }">
                  <span>{{ row.materialCode ? row.materialCode : "--" }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="物料名称"
                prop="materialName"
                :show-overflow-tooltip="true"
                min-width="160"
              />
              <el-table-column
                label="出厂型号"
                prop="factoryCode"
                :show-overflow-tooltip="true"
                min-width="120"
              />
              <el-table-column
                label="技术参数"
                prop="technologyParameter"
                :show-overflow-tooltip="true"
                min-width="120"
              />
              <el-table-column
                label="单位"
                prop="materialUnitName"
                :show-overflow-tooltip="true"
                min-width="60"
              />

              <el-table-column
                label="采购单价（元）"
                prop="purchasePrice"
                :show-overflow-tooltip="true"
                min-width="120"
              />

              <el-table-column
                label="申请入库数量"
                prop="warehousingQuantity"
                min-width="120"
              >
              </el-table-column>
              <el-table-column label="已入库数量" prop="inQty" min-width="120">
                <template #default="{ row }">
                  <span>{{ row.inQty ? row.inQty : 0 }}</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <SelectionMaterial
      ref="selectionMaterialRef"
      v-if="selectionMaterialShow"
      @refreshReturnList="gethandleReturn"
      v-model:selectionMaterialShow="selectionMaterialShow"
    />

    <el-dialog
      :title="title"
      v-model="auditDialogShow"
      width="600px"
      append-to-body
    >
      <el-form-item label="审核备注：" label-width="90">
        <el-input
          style="width: 100%"
          v-model="auditRemark"
          placeholder=""
          type="textarea"
          maxlength="2000"
          clearable
        />
      </el-form-item>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            style="color: #fff; background-color: rgba(67, 207, 124, 1)"
            v-if="auditResult == 3"
            @click="saveCheck()"
            v-loading="loadingCheck"
            >同意</el-button
          >
          <el-button
            style="color: #fff; background-color: rgba(245, 108, 108, 1)"
            v-if="auditResult == 4"
            @click="saveCheck()"
            v-loading="loadingCheck"
            >拒绝</el-button
          >
          <el-button
            style="color: #fff; background-color: rgba(245, 108, 108, 1)"
            v-if="auditResult == 6"
            @click="saveCheck()"
            v-loading="loadingCheck"
          >
            驳回</el-button
          >
          <el-button style="background-color: #fff" @click="closeAudit()"
            >取 消</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import SelectionMaterial from "./selectionMaterial";
import Upload from "@/components/Upload/index";
import { getUserListApi } from "@/api/information/customer";
import { getDictsApi } from "@/api/dict";
import { autoComplete } from "@/api/pub";
import {
  showAddTemp,
  saveAddWarehouseApply,
  showEditTemp,
  saveEditWarehouseApply,
  checkInorderApi,
} from "@/api/stock/inorder";
import { ElMessage, ElMessageBox } from "element-plus";
const route = useRoute();
const emit = defineEmits([
  // 客户新增组件显隐
  "addShow",
  // 更新客户新增组件显隐
  "update:addShow",
  // 刷新父组件表格数据
  "refreshList",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});

// 折叠栏默认展开
const activeNames = ref(["1", "2", "3"]);
// 客户表单引用
const requestFormRef = ref();
// 联系人表单引用
const contactFormRef = ref();
// 联系人操作类型
const operateType = ref("");
// 新增联系人表单显隐
const addShow = ref(false);

const selectionMaterialRef = ref();
// 选择物料显隐
const selectionMaterialShow = ref(false);

const loadingCheck = ref(false);

// 选择审核
const auditDialogShow = ref(false);

// 详情加载效果
const detailInfoShow = ref(false);
// 输入框引用
const refInput = ref();
// 下拉框引用
const refSelect = ref();
// 联系人当前索引(编辑联系人使用)
const tableIndex = ref(null);
// 采购类型列表
const typeList = ref([]);
// 单位列表
const unitList = ref([]);
// 入库类型状态
const inStatusList = ref([]);

// 客户表单数据模板
const requestFormTemplate = {
  warehouseApplyId: null,
  remarks: null,
  status: null,
  warehousingApplyCode: null,
  batchCode: null,
  applicantId: null,
  applicantDate: null,
  warehousingApplyType: null,
  purchaseApplyId: null,
  purchaseApplyCode: null,
  deliveryOrderId: null,
  deliveryApplyCode: null,
  relationContractId: null,
  contractCode: null,
  supplierName: null,
  projectId: null,
  projectName: null,
  warehousingCheckStatus: null,
  deptId: null,
  deptName: null,
  type: null,

  fileList: [],
  dtlList: [],
};
// 联系人表单数据模板

const data = reactive({
  // 客户表单
  requestForm: JSON.parse(JSON.stringify(requestFormTemplate)),

  // 客户表单校验规则
  queryRules: {
    applicantDate: [
      { required: true, message: "请选择申请日期", trigger: "change" },
    ],
    applicantId: [
      { required: true, message: "请选择申请人", trigger: "change" },
    ],
    warehousingApplyType: [
      { required: true, message: "请选择入库类型", trigger: "change" },
    ],
  },
  autoType: "",
  auditRemark: "",
  auditResult: "",
  applyId: "",

  // 用户列表
  userList: [],
});

const {
  requestForm,
  queryRules,
  userList,
  autoType,
  auditRemark,
  auditResult,
  applyId,
} = toRefs(data);

const handleCheckBtn2 = (requestFormRef, type, title) => {
  auditRemark.value = "";
  if (type == 3) {
    auditRemark.value = "同意";
  }

  auditResult.value = type;
  (applyId.value = requestForm.value.warehouseApplyId),
    (auditDialogShow.value = true);
  loadingCheck.value = false;
};

const saveCheck = async () => {
  if (auditRemark.value === null || auditRemark.value === "") {
    ElMessage.error("审核备注不能为空");
    return;
  }

  const obj = {
    approvalStatus: auditResult.value,
    warehouseApplyId: applyId.value,
    auditRemark: auditRemark.value,
  };
  try {
    loadingCheck.value = true;
    const res = await checkInorderApi(obj);
    auditDialogShow.value = false;

    //loadingCheck.value =false;

    if (res.code === 200) {
      ElMessage.success(res.msg);
    } else {
      ElMessage.error(res.msg);
      return;
    }

    loadingCheck.value = false;
    emit("update:addShow", false);
    emit("refreshList", false);
  } catch (error) {
    loadingCheck.value = false;
    emit("update:addShow", false);
    emit("refreshList", false);

    // ElMessage.error(error);
  }
};

const closeAudit = () => {
  auditRemark.value = "";
  auditResult.value = "";
  applyId.value = "";
  auditDialogShow.value = false;
};

const handleCheckBtn = (requestFormRef, type, title) => {
  ElMessageBox.confirm(`确定${title}以上数据吗？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "confirmBtnClass",
    cancelButtonClass: "cancelBtnClass",
  })
    .then(async () => {
      const obj = {
        approvalStatus: type,
        warehouseApplyId: requestForm.value.warehouseApplyId,
      };
      const res = await checkInorderApi(obj);
      if (res.code === 200) {
        emit("update:addShow", false);
        emit("refreshList", false);
        ElMessage.success(res.msg);
        // contractForm.value = JSON.parse(JSON.stringify(contractFormTemplate));
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};

/**
 * @description: 返回
 */
const handleBack = () => {
  emit("update:addShow", false);
  emit("refreshList", false);
  requestForm.value = JSON.parse(JSON.stringify(requestFormTemplate));
};

/**
 * @description: 处理选择物料
 */
const handleMaterial = async (row, tabIndex) => {
  let type = 1; //默认取物料数据

  selectionMaterialShow.value = true;

  nextTick(() => {
    selectionMaterialRef.value.handleMaterial(row);
  });
};

/**
 * @description: 处理物料选择
 */
const handleSelectMaterial = (value) => {
  if (value) {
    //selectionMaterialShow.value = false;

    requestForm.value.dtlList.push(JSON.parse(JSON.stringify(value)));
  } else {
    ElMessage.info("请选择物料");
  }
};

const handleAutoCompleteFocus = (value) => {
  autoType.value = value;
};

/**
 * @description: 处理供应商、生产厂家选择
 */
const handleProjectSelect = (value) => {
  requestForm.value.projectId = value.id;
  requestForm.value.projectName = value.projectName;
};

const handleRequestSelect = (value) => {
  requestForm.value.purchaseApplyId = value.id;
  requestForm.value.purchaseApplyCode = value.purchaseApplyCode;
};

const handleContractSelect = (value) => {
  requestForm.value.relationContractId = value.id;
  requestForm.value.contractCode = value.purchaseContractCode;
  requestForm.value.supplierName = value.supplierName;
};

const handleOutOrderSelect = (value) => {
  requestForm.value.deliveryOrderId = value.id;
  requestForm.value.deliveryApplyCode = value.deliveryApplyCode;
};

const querySearch = (queryStr, cb) => {
  if (autoType.value == "project") {
    requestForm.value.projectId = "";
    requestForm.value.projectName = "";
  }

  if (autoType.value == "request") {
    requestForm.value.purchaseApplyId = "";
    requestForm.value.purchaseApplyCode = "";
  }

  if (autoType.value == "contract") {
    requestForm.value.relationContractId = "";
    requestForm.value.contractCode = "";
    requestForm.value.supplierName = "";
  }

  if (autoType.value == "outOrder") {
    requestForm.value.deliveryOrderId = "";
    requestForm.value.deliveryApplyCode = "";
  }

  if (queryStr === "null") {
    queryStr = null;
  }
  autoComplete(queryStr, autoType.value).then((response) => {
    const list = response;

    cb(list);
  });
};

/**
 * @description: 处理人员选择
 * @param {Number} value 选择值
 */
const handleUserChange = (value) => {
  if (value) {
    const selectUser = userList.value.find((item) => item.userId === value);
    if (selectUser) {
      requestForm.value.deptName = selectUser.dept.deptName;
    }
  } else {
    requestForm.value.deptName = "";
  }
};

/**
 * @description: 处理人员选择
 * @param {Number} value 选择值
 */
const handleApplyTypeChange = (value) => {
  requestForm.value.purchaseApplyId = "";
  requestForm.value.purchaseApplyCode = "";

  requestForm.value.relationContractId = "";
  requestForm.value.contractCode = "";
  requestForm.value.supplierName = "";

  requestForm.value.deliveryOrderId = "";
  requestForm.value.deliveryApplyCode = "";
  requestForm.value.dtlList = [];
};

/**
 * @description: 文件上传成功处理
 * @param {Object} response 文件信息
 */
function uploadSuccess(response) {
  requestForm.value.fileList.push({ ...response, businessType: 8 });
}

/**
 * @description: 删除文件
 * @param {Number} index 当前文件索引
 */
function uploadRemove(index) {
  requestForm.value.fileList.splice(index, 1);
}

/**
 * @description: 处理合计
 */
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    const values = data.map((item) => Number(item[column.property]));
    // 只对amount这一列进行总计核算。
    if (
      column.property === "warehousingQuantity" ||
      column.property === "inQty"
    ) {
      if (!values.every((value) => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
        // sums[index] = sums[index].toFixed(2); // 保留2位小数，解决小数列合计精度缺失的问题
        // sums[index] += "元";
      } else {
        sums[index] = "";
      }
    }
  });

  return sums;
};

/**
 * @description: 处理联系人表格操作
 * @param {String} type 操作类型
 * @param {Object} row  当前行数据
 * @param {Number} tabIndex 表格当前索引
 */
const handleOperate = (type, row, tabIndex) => {
  requestForm.value.dtlList.splice(tabIndex, 1);
};

/**
 * @description: 打开新增页面
 * @param {Number} id
 */
const showAdd = async () => {
  try {
    await showAddTemp(1).then((res) => {
      if (res.code === 200) {
        nextTick(() => {
          requestForm.value = res.data;
        });
      }
    });

    detailInfoShow.value = false;
  } catch (error) {
    detailInfoShow.value = false;
  }
};

/**
 * @description: 处理详情信息
 * @param {Number} id
 */
let checkStatus = ref(null);
let returnId = null;
const showEdit = async (row) => {
  checkStatus.value = row.checkStatus;
  try {
    returnId = row.id;
    await showEditTemp({
      id: row.id,
      type: props.pageType === "check" ? null : row.checkStatus,
    }).then((res) => {
      if (res.code === 200) {
        nextTick(() => {
          requestForm.value = res.data;
        });
      }
    });
    detailInfoShow.value = false;
  } catch (error) {
    detailInfoShow.value = false;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  // 类型
  await getDictsApi("erp_applicant_type").then((res) => {
    if (res.code === 200) {
      typeList.value = res.data;
    }
  });

  // 入库类型
  await getDictsApi("erp_warehousing_apply_type").then((res) => {
    if (res.code === 200) {
      inStatusList.value = res.data;
    }
  });

  await getUserListApi({}).then((res) => {
    if (res.length > 0) {
      userList.value = res;
    }
  });
}

//入库关闭时刷新
const gethandleReturn = async () => {
  await showEditTemp({ id: returnId }).then((res) => {
    if (res.code === 200) {
      nextTick(() => {
        requestForm.value = res.data;
      });
    }
  });
};

onMounted(() => {
  getDicts();
  if (props.pageType === "add") {
    nextTick(() => {
      //refSelect.value.focus();
    });
  }
});

defineExpose({
  showEdit,
  showAdd,
});
</script>
<style lang="scss" scoped>
.requestAdd {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  .request-info {
    padding: 20px 20px 5px 20px;
    background-color: #f0f0f0;
    margin: 10px 0;
  }
  .contact-info {
    padding-top: 20px;
  }

  :deep(.el-table) {
    th.el-table__cell {
      background: #f8f8f8 !important;
    }
  }
  :deep(.el-date-editor.el-input, .el-date-editor.el-input__wrapper) {
    width: 268px !important;
    height: 34px !important;
  }
  .search-input {
    width: 268px;
    height: 34px;
  }
  .image-content {
    width: 100%;
    line-height: 0px;
    overflow: auto;
  }
}
.auto-top {
  width: 100%;
  position: absolute;
  top: 0px;
  display: flex;
  font-weight: bold;
}
.auto-item {
  width: 125px;
  text-align: center;
  white-space: nowrap; /* 禁止换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 使用省略号表示超出部分 */
}
.el-autocomplete .el-autocomplete-suggestion {
  max-height: 200px; /* 设置最大高度 */
  overflow-y: auto; /* 超出高度时显示滚动条 */
}
</style>
